import React from 'react';
import { observer, inject } from 'mobx-react';
import { getEcCom,getLabel } from '../../util/convert';
import * as _ from '../../util/public/common';
import * as FieldUtil from '../../util/public/fieldUtil';
import * as DetailUtil from '../../util/public/detailUtil';

@inject('globalStore', 'layoutStore')
@observer
export default class FileElement extends React.Component{
    fileChangeEvent = (ids,filedatas) => {
        let { layoutStore,fieldMark,fieldObj,rowIndex,baseProps } = this.props;
        const specialobj = fieldObj.specialobj || {};
        specialobj.filedatas = filedatas;
        layoutStore.changeSingleFieldValue(fieldMark, {value:ids.join(","), specialobj:specialobj});
	}
	onUploading = (type) => {
		let valueInfo = null;
        const { layoutStore,fieldMark,fieldObj,rowIndex,baseProps } = this.props;
        const { fieldid,isDetail } = baseProps;
		const fieldVars = {};
		//type = true 标记改字段值为空 type = false 标记该字段值不为空 这个时候不需要更新状态
		if(type == "false" && fieldObj.value == ""){
			valueInfo = {value:"-1"};
		}else if(type == "true"){
			valueInfo = {value:""};
		}
		if(type != "false" && type != "true"){
            const {variable = {}} = fieldObj;
            const {fileUploadStatus} = variable;
            if(fileUploadStatus != 'error') {
                fieldVars.fileUploadStatus = type;
            }
        }
        layoutStore.changeSingleFieldValue(fieldMark, valueInfo, fieldVars);
        if(isDetail){
            DetailUtil.syncLockedRowHeight(fieldid, rowIndex);
        }
	}
    render() {
        const { WeaUpload } = getEcCom();
        const { globalStore,layoutStore,fieldMark,fieldAttr,symbol,rowIndex,cellObj,fieldObj } = this.props;
        let { baseProps } = this.props;
        const { isDetail,fieldName,value,viewAttr,fieldid } = baseProps;
        const { detailtype,fileattr } = fieldAttr;
        let specialobj  = fieldObj.specialobj ? mobx.toJS(fieldObj.specialobj) : {};
        const { filedatas,showBatchLoad } = specialobj;
        //计算附件上传目录  catelogType = 0:固定目录 1：选择目录
        let category = "";
        let errorMsg = "";
        let { catelogType,docCategory,selectedCateLog,maxUploadSize }  = fileattr;
        if(catelogType === 0){
            if(_.trim(docCategory).length > 0){
                category = _.trim(docCategory);
            }else{
                errorMsg = getLabel(83157,"附件上传目录未设置!");
            }
        }else{
            const selectedCatalog = this.getFileSelectedCatalog();
            if(selectedCatalog.errorMsg){
                errorMsg = selectedCatalog.errorMsg;
            }else{
                category = selectedCatalog && selectedCatalog.docCategory;
                maxUploadSize = selectedCatalog && selectedCatalog.maxUploadSize;
            }
        }
        const { commonParam } = globalStore;
        const { workflowid,requestid,authStr,authSignatureStr,f_weaver_belongto_userid,f_weaver_belongto_usertype,wfTestStr,isprint } = commonParam;
        const listType = detailtype === 2 ? "img" : "list";
        const urlParams  = `&authStr=${authStr}&authSignatureStr=${authSignatureStr}&f_weaver_belongto_userid=${f_weaver_belongto_userid}&f_weaver_belongto_usertype=${f_weaver_belongto_usertype}&wfTestStr=${wfTestStr}`;
        let uploadUrl = `/api/doc/upload/uploadFile2Doc?category=${category}&workflowid=${workflowid}&listType=${listType}${urlParams}`;
        baseProps.showBatchLoad = isDetail ? false: showBatchLoad;
        baseProps.showClearAll = !isDetail;
        baseProps.uploadUrl = uploadUrl;
        baseProps.maxUploadSize = maxUploadSize;
        baseProps.category = category;
        filedatas && filedatas.map(item =>{
            if(viewAttr > 1 && layoutStore.propFileCfg.files_link_onlyview === "1"){
                //formFieldId=${fieldid}&formFieldAttr=2 控制当前这个附件文档是否可编辑 后续需要修改为接口,临时处理
                item.filelink = `${item.filelink}&isEdit=1&formFieldId=${fieldid}&formFieldAttr=2`;
            }
        });
        baseProps.datas = filedatas;
        baseProps.btnSize = isDetail ? "small": "default";
        baseProps.autoUpload = isDetail;
        baseProps.listType = listType;
        baseProps.uploadId = fieldName;
        baseProps.errorMsg = errorMsg;
        if(showBatchLoad && !isDetail){
            let batchLoadUrl  = `/weaver/weaver.file.FileDownload?fieldvalue=${value}&download=1&downloadBatch=1&desrequestid=0&requestid=${requestid}${urlParams}`;
            baseProps.batchLoadUrl = batchLoadUrl;
        }
        //字段是否开启拖拽， 后台是否开启拖拽，文件是否可编辑
        const { issortable= true, isresetname = true,isshowclerarall = false,isautoupload = true} = fieldAttr;
        //是否显示清楚全部
        baseProps.showClearAll = isshowclerarall;
        baseProps.issortable = issortable;
        baseProps.editable = isresetname;
        baseProps.autoUpload = isautoupload;//自动上传
        if(isautoupload) {//自动上传则存为草稿 ,同时需要不丢失其他属性
            baseProps.uploadParams = {isFirstUploadFile : "9", ...baseProps.uploadParams};//流程草稿
        }

        if(fileattr.imgHeight===0){
            fileattr.imgHeight=50;
        }
 
        if(fileattr.imgWidth===0){
            fileattr.imgWidth=50;
        }



        return(
            <WeaUpload ecId={`${this && this.props && this.props.ecId || ''}_WeaUpload@li5935`}
                ref={fieldName}
                uploadId={fieldName}
                {...{...fileattr,...baseProps}}
                needCalculateWidth={isprint != "1"}
                onChange={this.fileChangeEvent}
                onUploading={this.onUploading}
            />
        )
    }
    //获取附件字段对应的选择框目录信息
    getFileSelectedCatalog(){
        const { layoutStore,rowIndex,fieldAttr } = this.props;
        const { fileattr } = fieldAttr || {};
        const { fieldAttrMap } = layoutStore;
        const selectedFieldid = fileattr.selectedCateLog;
        if(fileattr.catelogType !== 1 || !selectedFieldid || !fieldAttrMap.has(selectedFieldid))      //附件字段非选择框控制目录
            return {};

        const selectedFieldValue = FieldUtil.getRelateFieldValue(selectedFieldid, rowIndex);
        const selectedFieldAttr = fieldAttrMap.get(selectedFieldid);
        const { fieldlabel,selectattr } = selectedFieldAttr;

        let selectedCatalog = {};
        if(selectedFieldid !== "" && selectedFieldValue !== ""){
            selectattr && selectattr.selectitemlist.map(v =>{
                if(v && v.selectvalue.toString() === selectedFieldValue.toString())
                    selectedCatalog = {docCategory:v.docCategory, maxUploadSize:v.maxUploadSize||5};
            });
        }else{
        	selectedCatalog.errorMsg = `${getLabel(125389,'请先选择')}“${fieldlabel}”`;
        }
        return selectedCatalog;
    }
}
